package com.exception;

import com.resp.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ControllerAdvice：给@Controller提供的建议
 *      当Controller层的代码抛出异常时，会自动调用此方法处理
 * @RestControllerAdvice= @ControllerAdvice+@ResponseBody
 */
@RestControllerAdvice  //用于标识当前类为REST风格对应的异常处理器
public class ProjectExceptionAdvice {

    //统一处理所有的Exception异常
    @ExceptionHandler(Exception.class)
    public Result doOtherException(Exception ex){
        ex.printStackTrace();
        return new Result(4001,"服务器异常，请稍后再试");
    }

    //处理空指针异常
    @ExceptionHandler(ArithmeticException.class)
    public Result doOtherException(ArithmeticException ex){
        System.out.println("计算异常-------");
        ex.printStackTrace();
        return new Result(4002,"计算异常，请稍后再试");
    }

    //自定义异常：业务层异常
    @ExceptionHandler(BusinessException.class)
    public Result doOtherException(BusinessException ex){
        ex.printStackTrace();
        return new Result(4003,"业务异常，请稍后再试");
    }


}